<template>
	<view class="all-main">
		<u-loading-page :loading="loadding" loading-mode="semicircle" loading-text="欢迎进入风动社区"></u-loading-page>
		<view v-if="!loadding" class="main">
			<view class="con">
				<view class="items">
					<view class="lefts">
						产品
					</view>
					<view class="rights" @click="proShow = true">
						{{product_name == '' ? '请选择产品' : product_name}}
					</view>
				</view>
				<u-line></u-line>
				<view class="items">
					<view class="lefts">
						数量
					</view>
					<view class="rights">
						<u-number-box v-model="number" :step="1" :min="0"></u-number-box>
					</view>
				</view>
				<u-picker :show="proShow" ref="uPicker" :columns="proList" keyName="name" @confirm="confirm" @cancel="proShow = false"></u-picker>
			</view>
			<view class="btn" @click="saveData">
				申请
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadding: true,
				proShow: false,
				product_id: '',
				product_name: '',
				proList: [],
				number: 0,
				customerinfo: {},
				is_true: true
			}
		},
		onLoad() {
			this.customerinfo = uni.getStorageSync('customerinfo');
			this.getProduct()
		},
		methods: {
			// 获取产品
			async getProduct() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'v1/getProductList',
					header: {
						'token': this.customerinfo.token
					}
				});
				this.loadding = false
				if (res.code == 1) {
					this.proList = [res.data]
				}
			},
			// 选择产品
			confirm(e) {
				this.proShow = false
				this.product_id = e.value[0].id
				this.product_name = e.value[0].name
			},
			// 保存信息
			async saveData() {
				if (this.product_id == '') {
					uni.showToast({
						title: '请选择产品',
						icon: "none"
					});
					return false
				}
				if (this.number <= 0) {
					uni.showToast({
						title: '请输入数量',
						icon: "none"
					});
					return false
				}
				var that = this
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp5({
						url: 'v1/saveTerminalApply',
						data: {
							product_id: this.product_id,
							number: this.number
						},
						header: {
							'token': this.customerinfo.token
						}
					});
					this.is_true = true
					uni.hideLoading()
					uni.showToast({
						title: '提交成功',
						icon: "none"
					});
					if (res.code == 1) {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1, // 返回的页面数，如果为1表示返回上一页
							});
						},1500)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f4f6;
	}
	
	.all-main {
		.main {
			padding: 20rpx;
			.con {
				background-color: #ffffff;
				.items {
					padding: 30rpx;
					display: flex;
					justify-content: space-between;
					.lefts {
						width: 20%;
					}
					.rights {
						display: flex;
						justify-content: flex-end;
						align-items: center;
						width: 80%;
						input {
							width: 100%;
							text-align: right;
						}
					}
				}
			}
			.btn {
				margin-top: 50rpx;
				background-color: #2979ff;
				color: #ffffff;
				font-weight: bold;
				text-align: center;
				padding: 30rpx;
				border-radius: 16rpx;
			}
		}
	}
</style>
